#include <iostream>
using namespace std;
int main ()
{int p,e,i,d,k;
 for(int l=1;;l++)
 {cin>>p>>e>>i>>d;
  if(p==-1&&e==-1&&i==-1&&d==-1) break;
  p=p%23;
  e=e%28;
  i=i%33;
  for(int j=33;;j++)
  {if(p==e==i&&d<p)    {k=p;break;}
   if(j%23==p&&j%28==e&&j%33==i&&(j-d)!=0) {k=j;break;}
   }
  cout<<"Case "<<l<<": the next triple peak occurs in "<<k-d<<" days."<<endl;
 }
 system("pause");
 return 0;
}
